﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zatify.Domain.Abstract;

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using Zatify.Domain.Data;

namespace Zatify.Domain.Concrete
{
    public class ZatifyReportGenerator //: IZatifyReportGenerator
    {
        public static byte[] GenerateUsersReport(List<User> users)
        {
            var workStream = new MemoryStream();

            var document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            var table = new PdfPTable(6);
            table.TotalWidth = 286f;

            var bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
            var timesHeader = new Font(Font.FontFamily.UNDEFINED, 12, Font.BOLD);
            var times = new Font(bfTimes, 10, Font.NORMAL);

            var cell = new PdfPCell(new Phrase("User ID", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Email", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Phone", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Pushes", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Check Ins", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Status", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            foreach (var user in users)
            {
                cell = new PdfPCell(new Phrase(user.Login, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(user.Email, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(user.Phone, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(user.Pushes.ToString(), times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(user.CheckIns.ToString(), times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(user.IsActive ? new Phrase("Active", times) : new Phrase("Inactive", times)));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
            }

            document.Add(table);

            document.Close();


            byte[] byteInfo = workStream.ToArray();

            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return workStream.ToArray();
        }

        public static byte[] GenerateSystemUsersReport(List<SystemUser> systemUsers)
        {
            var workStream = new MemoryStream();

            var document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            var table = new PdfPTable(5);
            table.TotalWidth = 286f;

            var bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
            var timesHeader = new Font(Font.FontFamily.UNDEFINED, 12, Font.BOLD);
            var times = new Font(bfTimes, 10, Font.NORMAL);

            var cell = new PdfPCell(new Phrase("User ID", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Email", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Phone", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Role", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Status", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            foreach (var systemUser in systemUsers)
            {
                cell = new PdfPCell(new Phrase(systemUser.Login, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(systemUser.Email, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(systemUser.Phone, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(systemUser.Role, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(systemUser.IsActive ? new Phrase("Active", times) : new Phrase("Inactive", times)));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
            }

            document.Add(table);

            document.Close();


            byte[] byteInfo = workStream.ToArray();

            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return workStream.ToArray();
        }

        public static byte[] GenerateUserReferrerReport(List<Report_UserReferrer_Result> userReferrers)
        {
            var workStream = new MemoryStream();

            var document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            var table = new PdfPTable(7);
            table.TotalWidth = 286f;

            var bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
            var timesHeader = new Font(Font.FontFamily.UNDEFINED, 12, Font.BOLD);
            var times = new Font(bfTimes, 10, Font.NORMAL);

            var cell = new PdfPCell(new Phrase("ID", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Referred by ID", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Referred ID", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Referred ID UserType", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Date", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("No", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Total", timesHeader));
            cell.VerticalAlignment = 1;
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

            foreach (var userReferrer in userReferrers)
            {
                cell = new PdfPCell(new Phrase(userReferrer.ID.ToString(), times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(userReferrer.Referred_by_ID, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(userReferrer.Referred_ID, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(userReferrer.Referred_ID_UserType, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 0;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(userReferrer.Date, times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 2;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(userReferrer.No.ToString(), times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 2;
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(userReferrer.Total.ToString(), times));
                cell.VerticalAlignment = 1;
                cell.HorizontalAlignment = 2;
                table.AddCell(cell);
            }

            document.Add(table);

            document.Close();


            byte[] byteInfo = workStream.ToArray();

            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return workStream.ToArray();
        }
    }
}
